package dyy.zc.alumnibook.ui.adapter;

import android.content.Context;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import dyy.zc.alumnibook.R;
import dyy.zc.alumnibook.bean.Friend;
import dyy.zc.alumnibook.listener.OnItemClickListener;
import dyy.zc.alumnibook.listener.OnItemFriendClick;
import dyy.zc.alumnibook.utils.Utils;

/**
 * Created by chuan on 2017/4/15.
 */

public class FriendAdapter extends RecyclerView.Adapter<FriendAdapter.MyViewHolder> {
    private Context context;
    private List<Friend> list;
    private OnItemClickListener onItemClickListener;
    private OnItemFriendClick onItemFriendClick;
    private List<Boolean> switchZu = new ArrayList<>();
    public FriendAdapter(Context context,List<Friend> list,OnItemFriendClick onItemFriendClick){
        this.context = context;
        this.list = list;
        this.onItemFriendClick = onItemFriendClick;
        for (Friend friend:list){
            switchZu.add(false);
        }
    }

    public void setList(List<Friend> list) {
        this.list = list;
        for (Friend friend:list){
            switchZu.add(false);
        }
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_friend_zu,null),onItemClickListener);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        holder.name.setText(list.get(position).getZuming());
        if (switchZu.get(position)){
            holder.recyclerView.setVisibility(View.VISIBLE);
        }else {
            holder.recyclerView.setVisibility(View.GONE);
        }
        FriendListAdapter adapter = new FriendListAdapter(context,list.get(position).getHead(),
                list.get(position).getName(),position,onItemFriendClick);
        holder.recyclerView.setLayoutManager(new LinearLayoutManager(context));
        holder.recyclerView.setItemAnimator(new DefaultItemAnimator());
        holder.recyclerView.setAdapter(adapter);
        holder.name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (switchZu.get(position)){
                    holder.recyclerView.setVisibility(View.GONE);
                    switchZu.set(position,false);
                }else {
                    holder.recyclerView.setVisibility(View.VISIBLE);
                    switchZu.set(position,true);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        if (list == null || list.size() == 0){
            return 0;
        }
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private OnItemClickListener onItemClickListener;
        private TextView name;
        private RecyclerView recyclerView;
        public MyViewHolder(View itemView,OnItemClickListener onItemClickListener) {
            super(itemView);
            itemView.setOnClickListener(this);
            this.onItemClickListener = onItemClickListener;
            name = Utils.findViewsById(itemView,R.id.name);
            recyclerView = Utils.findViewsById(itemView,R.id.recyclerView);
        }

        @Override
        public void onClick(View v) {
            if (onItemClickListener !=null){
                onItemClickListener.onItemClick(getAdapterPosition());
            }
        }
    }

}
